package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.MallDeliveryAddress;
import com.sc.nft.entity.vo.MallDeliveryAddressVO;
import com.sc.nft.mapper.MallDeliveryAddressMapper;
import com.sc.nft.sup.PageRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

/**
 * 收货地址表(MallDeliveryAddress)表Dao层
 *
 * @author wwc
 * @since 2023-05-11 16:35:34
 */
@Slf4j
@Repository
public class MallDeliveryAddressDao extends ServiceImpl<MallDeliveryAddressMapper, MallDeliveryAddress> {

    public MallDeliveryAddress getAddressByUserIdAndId(Long userId, Long id) {
        LambdaQueryWrapper<MallDeliveryAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallDeliveryAddress::getUserId, userId);
        wrapper.eq(MallDeliveryAddress::getId, id);
        return this.getOne(wrapper);
    }

    /**
     * 所有默认地址关闭
     *
     * @param userId
     */
    public void updateDefaultAddress(Long userId) {
        UpdateWrapper<MallDeliveryAddress> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        updateWrapper.set("default_address", 0);
        this.update(updateWrapper);
    }

    /**
     * 分页查询地址列表
     *
     * @param pageRequest
     * @return
     */
    public Page<MallDeliveryAddressVO> getPageData(PageRequest pageRequest,Long userId) {
        return baseMapper.getPageData(pageRequest.toMp(),userId);
    }

    /**
     * 获取默认地址
     *
     * @param userId
     * @return
     */
    public MallDeliveryAddressVO getDefaultAddress(Long userId) {
        return baseMapper.getDefaultAddress(userId);
    }

}
